/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.huawei.spare.part.replace.views.replace.perform;

import com.huawei.spare.part.replace.framework.SmartContentPresenter;
import com.huawei.spare.part.replace.framework.SmartDataBus;
import com.huawei.spare.part.replace.models.Uplink;
import com.huawei.spare.part.replace.views.widgets.cell.EditCell;
import io.reactivex.Observable;
import java.util.List;
import java.util.stream.Collectors;
import javafx.application.Platform;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Control;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.text.Text;
import javafx.util.Callback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Woo
 */
public class PopupUplinkPresenter extends SmartContentPresenter {

  private static final Logger log = LoggerFactory.getLogger(
      PopupUplinkPresenter.class);

  @FXML TableView uplinkTable;
  @FXML TableColumn<Uplink, String> switchPortColumn;
  @FXML TableColumn<Uplink, String> businessPortColumn;
  @FXML TableColumn<Uplink, String> remarkColumn;

  ObservableList<Uplink> uplinks = FXCollections.observableArrayList();

  @Override
  public void initialize() {
      initialTableView();
  }

  /**
   * 初始化表格渲染
   */
  private void initialTableView() {
    // Cell selectable
    uplinkTable.getSelectionModel().cellSelectionEnabledProperty().set(true);

    switchPortColumn.setCellValueFactory(new PropertyValueFactory("switchPort"));
    businessPortColumn.setCellValueFactory(new PropertyValueFactory("businessPort"));
    remarkColumn.setCellValueFactory(new PropertyValueFactory("remark"));

    switchPortColumn.setCellFactory(getWrappedTextCell());
    businessPortColumn.setCellFactory(getWrappedTextCell());
    remarkColumn.setCellFactory(getWrappedTextCell());

    uplinkTable.setItems(FXCollections.observableArrayList(SmartDataBus.get().replacement().getUplinks()));
  }

  private Callback<TableColumn<Uplink, String>, TableCell<Uplink, String>> getWrappedTextCell() {
    return tc -> {
      TableCell<Uplink, String> cell = new TableCell<>();
      Text text = new Text();
      cell.setGraphic(text);
      cell.setPrefHeight(Control.USE_COMPUTED_SIZE);
      text.wrappingWidthProperty().bind(tc.widthProperty());
      text.textProperty().bind(cell.itemProperty());
      return cell;
    };
  }

  @Override
  public Observable<Boolean> onFinish() {
    return Observable.just(true);
  }
}
